//
//  BSLZIP.m
//  HelloWorld
//
//  Created by LYK on 15/7/29.
//
//

#import "BSLZIP.h"
#import "Main.h"
@implementation BSLZIP
- (void)zip:(CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
//        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *dirPaths = command.arguments[0];
        BOOL isSuccess = NO;
        CDVPluginResult *result = nil;
        NSString *targetPath=[NSString stringWithFormat:@"%@",command.arguments[1][@"target"]];
        if ( [[command.arguments[1] allKeys] count]>1) {
            if ([targetPath hasSuffix:@"/"]) {
               targetPath=[NSString stringWithFormat:@"%@%@",targetPath,command.arguments[1][@"name"]];
            }else{
            targetPath=[NSString stringWithFormat:@"%@/%@",targetPath,command.arguments[1][@"name"]];
            }
        }else{
            targetPath=[NSString stringWithFormat:@"%@/%@.zip",targetPath,[[dirPaths componentsSeparatedByString:@"/" ]  lastObject]];
        }
        if ([dirPaths hasPrefix:@"file://"]) {
          dirPaths=  [dirPaths substringFromIndex:7];
        }
        if ([targetPath hasPrefix:@"file://"]) {
           targetPath= [targetPath substringFromIndex:7];
        }
      
        
            isSuccess = [Main createZipFileAtPath:targetPath withContentsOfDirectory:dirPaths];
    
        
        if (isSuccess) {
            result = [CDVPluginResult resultWithStatus:
                      CDVCommandStatus_OK];
        }else{
            result = [CDVPluginResult resultWithStatus:
                      CDVCommandStatus_ERROR];
        }
        
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    }];
}

- (void)unzip:(CDVInvokedUrlCommand *)command{
    [self.commandDelegate runInBackground:^{
//        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//        NSString *filePath = [docPath stringByAppendingPathComponent:command.arguments[0]];
//        NSString *destPath = [docPath stringByAppendingPathComponent:command.arguments[1]];
        
        NSString *filePath = command.arguments[0];
        CDVPluginResult *result = nil;
        NSString *destPath=[NSString stringWithFormat:@"%@",command.arguments[1][@"target"]];
        if ( [[command.arguments[1] allKeys] count]>1) {
            if ([destPath hasSuffix:@"/"]) {
                destPath=[NSString stringWithFormat:@"%@%@",destPath,command.arguments[1][@"name"]];
            }else{
                destPath=[NSString stringWithFormat:@"%@/%@",destPath,command.arguments[1][@"name"]];
            }
        }else{
        
        }
        if ([destPath hasPrefix:@"file://"]) {
           destPath=  [destPath substringFromIndex:7];
        }
        if ([filePath hasPrefix:@"file://"]) {
           filePath= [filePath substringFromIndex:7];
        }
        BOOL isSuccess = [Main unzipFileAtPath:filePath toDestination:destPath];
        
        if (isSuccess) {
            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        }else{
            result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
        }
        
        [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
    }];
}
- (void)createFile:(CDVInvokedUrlCommand *)command{
    NSString *test = @"test file!";
    NSError *error = nil;
    [test writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"test.txt"] atomically:YES encoding:NSUTF8StringEncoding error:&error];
    
    CDVPluginResult *result = nil;
    if (!error) {
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    }else{
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
@end
